package com.yiwenup.struct._03_collect.set;

import com.yiwenup.struct._02_tree.bst.bbst.RBTree;
import com.yiwenup.struct._03_collect.set.base.Set;

import java.util.Comparator;

/**
 * 红黑树实现
 **/
public class TreeSet<E> implements Set<E> {
    private RBTree<E> rb;

    public TreeSet() {
        this(null);
    }

    public TreeSet(Comparator<E> comparator) {
        rb = new RBTree<>(comparator);
    }

    @Override
    public int size() {
        return rb.size();
    }

    @Override
    public boolean isEmpty() {
        return rb.isEmpty();
    }

    @Override
    public void clear() {
        rb.clear();
    }

    @Override
    public boolean contains(E element) {
        return rb.contains(element);
    }

    @Override
    public void add(E element) {
        rb.add(element);
    }

    @Override
    public void remove(E element) {
        rb.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if (visitor == null) return;
        rb.inorderTraversal(visitor::visit);
    }
}
